package com.txzhang.springcloud.accountservice.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.txzhang.springcloud.accountservice.entity.Account;
import com.txzhang.springcloud.accountservice.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/json")
    public Object json(Integer id) {
        return accountService.getById(id);
    }

    /**
     * 根据用户id，扣除对应的钱
     * @param userId
     * @param money
     * @return
     */
    @RequestMapping("/deduction")
    public R deduction(Long userId, Integer money) {
        Account account = accountService.getById(userId);
        if (account.getBalance() < money) {
            throw new RuntimeException("账户余额不足");
        }
        account.setBalance(account.getBalance() - money);
        accountService.saveOrUpdate(account);
        return R.ok("扣款成功！");
    }

    /**
     * 根据用户id获取 账户余额
     * @param userId
     * @return
     */
    @GetMapping("/getBalanceByUserId")
    public R getBalanceByUserId(Integer userId) {
        return null;
    }
}
